# 空字典
empty_dict = {}
 
# 包含键值对的字典
person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}
 
print(empty_dict)  # 输出：{}
print(person)      # 输出：{'name': 'Alice', 'age': 25, 'city': 'New York'}


person = {"name": "Alice", "age": 25, "city": "New York"}
 
# 访问字典值
print(person["name"])  # 输出：Alice
print(person["age"])   # 输出：25
 
# 如果键不存在，会报错
# print(person["gender"])  # KeyError: 'gender'

person = {"name": "Alice", "age": 25}
 
# 安全访问
print(person.get("name"))    # 输出：Alice
print(person.get("gender"))  # 输出：None
print(person.get("gender", "Not Specified"))  # 输出：Not Specified

person = {"name": "Alice", "age": 25}
 
# 修改键的值
person["age"] = 26
print(person)  # 输出：{'name': 'Alice', 'age': 26}
 
# 新增键值对
person["gender"] = "Female"
print(person)  # 输出：{'name': 'Alice', 'age': 26, 'gender': 'Female'}


person = {"name": "Alice", "age": 25, "city": "New York"}
 
# 删除指定键
del person["age"]
print(person)  # 输出：{'name': 'Alice', 'city': 'New York'}
 
# 删除并返回值
city = person.pop("city")
print(city)    # 输出：New York
print(person)  # 输出：{'name': 'Alice'}
 
# 随机删除键值对
last_item = person.popitem()
print(last_item)  # 输出：('name', 'Alice')
print(person)     # 输出：{}
 
# 清空字典
person.clear()
print(person)  # 输出：{}


person = {"name": "Alice", "age": 25, "city": "New York"}
 
for key in person:
    print(key)
# 输出：
# name
# age
# city


for value in person.values():
    print(value)
# 输出：
# Alice
# 25
# New York

for key, value in person.items():
    print(f"{key}: {value}")
# 输出：
# name: Alice
# age: 25
# city: New York


person = {"name": "Alice", "age": 25}
 
# 获取所有键
print(person.keys())  # 输出：dict_keys(['name', 'age'])
 
# 获取所有值
print(person.values())  # 输出：dict_values(['Alice', 25])
 
# 获取所有键值对
print(person.items())  # 输出：dict_items([('name', 'Alice'), ('age', 25)])
 
# 更新字典
person.update({"city": "New York", "age": 26})
print(person)  # 输出：{'name': 'Alice', 'age': 26, 'city': 'New York'}
 
# 浅拷贝
new_person = person.copy()
print(new_person)  # 输出：{'name': 'Alice', 'age': 26, 'city': 'New York'}


# from collections import Counter
 
# text = "hello world"
# counter = Counter(text)
# print(counter)  # 输出：Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

from collections import Counter

text = "hello world"
counter = Counter(text)
print(counter)  # 输出：Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

# 统计字符出现次数
print(counter.most_common())  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]

# 统计字符出现次数      
print(counter.most_common(2))  # 输出：[('l', 3), ('o', 2)]
# 统计字符出现次数      
print(counter.most_common(1))  # 输出：[('l', 3)]
# 统计字符出现次数      
print(counter.most_common(0))  # 输出：[]
# 统计字符出现次数      
print(counter.most_common(-1))  # 输出：[]
# 统计字符出现次数      
print(counter.most_common(3))  # 输出：[('l', 3), ('o', 2), ('h', 1)]
# 统计字符出现次数      
print(counter.most_common(4))  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1)]
# 统计字符出现次数      
print(counter.most_common(5))  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1), ('w', 1)]
# 统计字符出现次数      
print(counter.most_common(6))  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1), ('w', 1), ('r', 1)]
# 统计字符出现次数      
print(counter.most_common(7))  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1), ('w', 1), ('r', 1), ('d', 1)]
# 统计字符出现次数      
print(counter.most_common(8))  # 输出：[('l', 3), ('o', 2), ('h', 1), ('e', 1), ('w', 1), ('r', 1), ('d', 1)]

